﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using XbsWebAPI.Core.Repositories.SystemAdmin;
using XbsWebAPI.EntityFrameworkCore;
using XbsWebAPI.EntityFrameworkCore.Entities;
using XbsWebAPI.EntityFrameworkCore.Entities.SystemAdmin;

namespace XbsWebAPI.Core.Repositories
{
    public class XbsAdminUserRepository : IXbsAdminUserRepository
    {
        private readonly DataContext _dbContext;

        public XbsAdminUserRepository(DataContext dataContext)
        {
            _dbContext = dataContext;
        }
        

        public IQueryable<XbsAdminUser> AsQueryable()
        {
           return _dbContext.Users.AsQueryable();
           
        }
        //更新数据库
        public XbsAdminUser Create(XbsAdminUser entity)
        {
            entity.Id = Guid.NewGuid().ToString();
            _dbContext.Add(entity);
            _dbContext.SaveChanges();

            return entity;
        }
        public Task<XbsAdminUser> CreateAsync(XbsAdminUser entity)
        {
            return Task.FromResult(Create(entity));
        }

        /// <summary>
        /// 弃用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public XbsAdminUser Delete(Guid id)
        {
            var model = _dbContext.Users.FirstOrDefault(x => x.Id == id.ToString());
            if (model == null)
                return null;
            _dbContext.Remove(model);
            _dbContext.SaveChanges();

            return model;
        }

        public Task<XbsAdminUser> DeleteAsync(string id)
        {
            throw new NotImplementedException();
        }

        public Task<XbsAdminUser> DeleteAsync(Guid id)
        {
            var model = _dbContext.Users.FirstOrDefault(x => x.Id == id.ToString());
            if (model == null)
                throw new Exception($"Id={id}的用户不存在，无法进行删除操作");

            _dbContext.Remove(model);
            _dbContext.SaveChanges();

            return Task.FromResult(model);
        }

        public IQueryable<XbsAdminUser> Query(Expression<Func<XbsAdminUser, bool>> filter)
        {
            var query = _dbContext.Users.Where(filter);

            return query;
        }

        /// <summary>
        /// 弃用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public XbsAdminUser QueryById(Guid id)
        {
            return _dbContext.Users.FirstOrDefault(x => x.Id == id.ToString());
        }

        public XbsAdminUser Update(XbsAdminUser entity)
        {
            var model = _dbContext.Users.AsNoTracking().FirstOrDefault(x => x.Id == entity.Id);
            if (model == null)
                return null;
            _dbContext.Users.Update(entity);
            _dbContext.SaveChanges();

            return entity;
        }

        public Task<XbsAdminUser> UpdateAsync(XbsAdminUser entity)
        {
            return Task.FromResult(Update(entity));
        }


    }
}
